import {_decorator, Component, log, mat4, Mat4, Node, Quat, quat, v3, Vec3} from 'cc';
import {NodeSpace} from "../../../engine_1.2.0/cocos/core/scene-graph/node-enum";

const { ccclass, property } = _decorator;


const quat_1 = quat();
const v3_1 = v3();
const mat_1 = mat4();

Quat.fromEuler(quat_1,0,2,0);

@ccclass('BaseControl')
export class BaseControl extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;

    @property({type:Node})
    nd_ball_a:Node = null;

    @property({type:Node})
    nd_ball_b:Node = null;

    start () {
        // Your initialization goes here.
    }


    move_btnClicked(){

        // 让B绕着A转
        // 1. A不动
        this.schedule(this.B_rot_A);
    }

    B_rot_A(){
        // 1. 将a的位置变换到b坐标系下
        // 2. T * R * T^-1
        // 3. 注意: node 的 inverseTransformPoint 方法有bug
        // 4. 是否有更简单的api?
        let worldPos_a = this.nd_ball_a.getWorldPosition();
        Mat4.invert(mat_1,this.nd_ball_b.worldMatrix);
        Vec3.transformMat4(v3_1,worldPos_a,mat_1);

        this.nd_ball_b.translate(v3_1,Node.NodeSpace.LOCAL);
        this.nd_ball_b.rotate(quat_1,Node.NodeSpace.LOCAL);
        this.nd_ball_b.translate(Vec3.negate(v3_1,v3_1),Node.NodeSpace.LOCAL);
    }
}
